package com.epoint.mobileoa.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;

/**
 * 
 * @author lilin
 * @date 2012-5-11 下午12:29:04
 * @annotation 已下载附件列表适配器
 */
public class AttachManagerListAdp extends BaseAdapter {
	private LayoutInflater mInflater;
	public static Map<Integer, Boolean> isSelected;
	private List<Map<String, Object>> recList;
	private int length;
	@SuppressWarnings("unused")
	private SuperView context;

	public AttachManagerListAdp(SuperView context,
			List<Map<String, Object>> recList) {
		this.context = context;
		this.recList = recList;
		length = recList.size();
		mInflater = LayoutInflater.from(context);
		// 这儿定义isSelected这个map是记录每个listitem的状态，初始状态全部为false。
		isSelected = new HashMap<Integer, Boolean>();
		for (int i = 0; i < length; i++) {
			isSelected.put(i, false);
		}
		// fileList = new ArrayList();
	}

	public int getCount() {
		return recList.size();
	}

	public Object getItem(int position) {
		return recList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder holder = null;
		// view为null的时候初始化view。
		if (view == null) {
			holder = new ViewHolder();
			view = mInflater.inflate(R.layout.listitem_attachdownload, null);
			holder.filename = (TextView) view.findViewById(R.id.filename);
			holder.checkBox = (CheckBox) view.findViewById(R.id.cb);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		context.setTextViewFontColor(holder.filename);
		holder.filename.setText(recList.get(position).get("title").toString());
		holder.checkBox.setChecked(isSelected.get(position));
		final CheckBox checkBox = holder.checkBox;
		final int arg2 = position;
		checkBox.setChecked(isSelected.get(position));
		checkBox.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// 记录CheckBox的状态
				if (isSelected.get(arg2)) {
					isSelected.put(arg2, false);
				} else {
					isSelected.put(arg2, true);
				}
				notifyDataSetChanged();
				// String msg = "";
				// // 记录CheckBox的状态
				// if (isSelected.get(arg2)) {
				// isSelected.put(arg2, false);
				// fileList.remove(recList.get(arg2).get("title").toString()
				// + ",");
				// } else {
				// isSelected.put(arg2, true);
				// fileList.add(recList.get(arg2).get("title").toString()
				// + ",");
				// msg += arg2 + ";";
				// }
				// WXZJ_MailSendView.selectAttachIndex = msg;
			}
		});
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
			}
		});
		return view;
	}

	public final class ViewHolder {
		public TextView filename;// 文件名
		public CheckBox checkBox;// 复选框
	}

}
